/***************************************************************
 * Name:      CamMain.cpp
 * Purpose:   Code for Application Frame
 * Author:    Riku Maikola (rpm@ac-mapa.org)
 * Created:   2011-02-17
 * Copyright: Riku Maikola (rpm.ac-mapa.org)
 * License:
 **************************************************************/
#include "wx/wxprec.h"
#include <wx/wx.h>

#include "TerminalMain.h"
#include <wx/dcmemory.h>

//CamMain::CamMain(wxFrame *frame) : CamMain(frame)
CamMain::CamMain( wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style ) : wxFrame( parent, id, title, pos, size, style )
{
    this->SetMinSize(wxSize(400,200));
    this->SetMaxSize(wxSize(400,200));
    statusBar = this->CreateStatusBar( 3, wxST_SIZEGRIP, wxID_ANY );

    //****************** MENU *************************//
    MenuBar = new wxMenuBar( 0 );

    //*********************
    ProjectMenu = new wxMenu();
    wxMenuItem* LoadMenu;
    LoadMenu = new wxMenuItem( ProjectMenu, menuLoad, wxString( wxT("Load") ) + wxT('\t') + wxT("F1"), wxEmptyString, wxITEM_NORMAL );
    ProjectMenu->Append( LoadMenu );

    wxMenuItem* SaveMenu;
    SaveMenu = new wxMenuItem( ProjectMenu, menuSave, wxString( wxT("Save") ) + wxT('\t') + wxT("F2"), wxEmptyString, wxITEM_NORMAL );
    ProjectMenu->Append( SaveMenu );

    wxMenuItem* SaveAsMenu;
    SaveAsMenu = new wxMenuItem( ProjectMenu, menuSaveAs, wxString( wxT("Save As") ) + wxT('\t') + wxT("F3"), wxEmptyString, wxITEM_NORMAL );
    ProjectMenu->Append( SaveAsMenu );

    MenuBar->Append( ProjectMenu, wxT("Project") );

    //CONNECT
    this->Connect( LoadMenu->GetId(), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( CamMain::OnMenu ) );
    this->Connect( SaveMenu->GetId(), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( CamMain::OnMenu ) );
    this->Connect( SaveAsMenu->GetId(), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( CamMain::OnMenu ) );



    //----------
    //****************** MENU *************************//
    EditorMenu = new wxMenu();
    wxMenuItem* EditCodeMenu;
    EditCodeMenu = new wxMenuItem( EditorMenu , menuEditCode , wxString( wxT("Code editor") ) + wxT('\t') + wxT("F5"), wxEmptyString, wxITEM_NORMAL );
    EditorMenu->Append( EditCodeMenu );
    wxMenuItem* EditWorkDirMenu;
    EditWorkDirMenu = new wxMenuItem( EditorMenu , menuEditWorkDir , wxString( wxT("Open Work directory") ) + wxT('\t') + wxT("F9"), wxEmptyString, wxITEM_NORMAL );
    EditorMenu->Append( EditWorkDirMenu );
    MenuBar->Append( EditorMenu, wxT("Edit") );

    this->Connect( EditCodeMenu->GetId(), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( CamMain::OnMenuEdit ) );
    this->Connect( EditWorkDirMenu->GetId(), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( CamMain::OnMenuEdit ) );

     //****************** BUILD MENU *************************//
    BuildMenu = new wxMenu();
    wxMenuItem* BuildBuildMenu;
    BuildBuildMenu = new wxMenuItem( BuildMenu , menuBuild , wxString( wxT("Build") ) + wxT('\t') + wxT("F10"), wxEmptyString, wxITEM_NORMAL );
    BuildMenu->Append( BuildBuildMenu );
    wxMenuItem* BuildBuildRunMenu;
    BuildBuildRunMenu = new wxMenuItem( BuildMenu , menuBuildRun , wxString( wxT("Build/Run") ) + wxT('\t') + wxT("F11"), wxEmptyString, wxITEM_NORMAL );
    BuildMenu->Append( BuildBuildRunMenu );
    wxMenuItem* BuildRunMenu;
    BuildRunMenu = new wxMenuItem( BuildMenu , menuRun , wxString( wxT("Run") ) + wxT('\t') + wxT("F12"), wxEmptyString, wxITEM_NORMAL );
    BuildMenu->Append( BuildRunMenu );
    MenuBar->Append( BuildMenu, wxT("Build") );

    this->Connect( BuildBuildMenu->GetId(), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( CamMain::OnMenuBuild ) );
    this->Connect( BuildBuildRunMenu->GetId(), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( CamMain::OnMenuBuild ) );
    this->Connect( BuildRunMenu->GetId(), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( CamMain::OnMenuBuild ) );


    //------------------
    this->SetMenuBar( MenuBar );



    // Connect Events
    this->Connect( wxEVT_CLOSE_WINDOW, wxCloseEventHandler( CamMain::OnClose ) );


    //**********************************************************************************************************//
    //**
    //**                CLI -
    iPAST = new wxTextCtrl( this , wxID_ANY, wxEmptyString, wxPoint(0,0) , wxSize( 400,135 ), wxTE_MULTILINE|wxTE_READONLY|wxRAISED_BORDER );
    iCLI  = new wxTextCtrl( this , wxID_ANY, wxEmptyString, wxPoint(0,135) , wxSize( 400,20 ), wxTE_PROCESS_ENTER );
    iCLI->Connect( wxEVT_COMMAND_TEXT_ENTER, wxCommandEventHandler( CamMain::iEXE ), NULL, this );

}
CamMain::~CamMain()
{
    // Disconnect Events
    this->Disconnect( wxEVT_CLOSE_WINDOW, wxCloseEventHandler( CamMain::OnClose ) );
    iCLI->Disconnect( wxEVT_COMMAND_TEXT_ENTER, wxCommandEventHandler( CamMain::iEXE ), NULL, this );

    this->Disconnect( wxID_ANY, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( CamMain::OnMenu ) );
    this->Disconnect( wxID_ANY, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( CamMain::OnMenuEdit ) );
    this->Disconnect( wxID_ANY, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( CamMain::OnMenuBuild ) );


    //clear working directory
    std::string save = "rm -rf ";
    save += WORKDIR.mb_str();
    save += "\n";
    uint ulos = system(save.c_str());


}
//***************************************//
//**
//***********************************************//
void CamMain::OnMenu( wxCommandEvent& event )
{
    if(event.GetId()==menuLoad)
    {
        wxFileDialog LoadFileDialog(this, _("Load Project"), _(""),PATH,_("cam files (*.cam)|*.cam"), wxFD_OPEN|wxFD_FILE_MUST_EXIST);
        if (LoadFileDialog.ShowModal() == wxID_CANCEL)
            return;     // the user changed idea...

        //
        PATH = LoadFileDialog.GetPath();

        //Clear all
        std::string load = "rm -rf ";
        load += WORKDIR.mb_str();
        load += "\n";
        int ulos = system(load.c_str());

        std::string dir;

        //system(code.c_str());
        for(int a=0;a<1000;a++)
        {
            dir ="/dev/shm/maicam_";

            std::ostringstream convert;
            convert << a;
            dir += convert.str();
            if( mkdir(dir.c_str(),S_IRWXU|S_IRGRP|S_IXGRP) == 0)break;
        }
        WORKDIR = wxString::FromUTF8(dir.c_str());

        std::string code;
        code = "cd ";
        code += WORKDIR.char_str();
        code += "\ntar -xf ";
        code += PATH.mb_str();
        ulos = system(code.c_str());
        SetTitle(PATH);
        return;
    }

    //SAVE
    else if(event.GetId()==menuSave)
    {
        std::string save = "cd ";
        save += WORKDIR.mb_str();
        save += "\ntar -cf ";
        save += PATH.mb_str();
        save += " *\n";
        int ulos = system(save.c_str());
        return;
    }

    else if(event.GetId()==menuSaveAs)
    {
        wxFileDialog SaveFileDialog(this, _("Save project"), _(""), _(""),_("cam files (*.cam)|*.cam"),wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
        SaveFileDialog.SetPath(PATH);
        if (SaveFileDialog.ShowModal() == wxID_CANCEL)
            return;     // the user changed idea...

        PATH =  SaveFileDialog.GetPath();
        SetTitle(PATH);

        std::string save = "cd ";
        save += WORKDIR.mb_str();
        save += "\ntar -cf ";
        save += PATH.mb_str();
        save += " *\n";
        int ulos = system(save.c_str());
        return;
    }
}

//-----------------------------------------------
void CamMain::OnMenuEdit( wxCommandEvent& event )
{
    if(event.GetId()==menuEditCode)
    {
        std::string edit = "gedit ";
        edit += WORKDIR.mb_str();
        edit += "/CODE &\n";
        uint a = system(edit.c_str());
    }

    if(event.GetId()==menuEditWorkDir)
    {
        std::string edit = "thunar ";
        edit += WORKDIR.mb_str();
        edit += "&\n";
        uint a = system(edit.c_str());
    }
}
//------------------------------------------------
void CamMain::OnMenuBuild( wxCommandEvent& event )
{
    if(event.GetId()==menuRun)
    {
        std::string edit = "/opt/MAICAM/c/simulator ";
        edit += WORKDIR.mb_str();
        edit += "&\n";
        uint a = system(edit.c_str());
    }
}

//-----------------------------------------
void CamMain::iEXE( wxCommandEvent& event )
{

    std::cout << "hitĵ\n";
/*    E(iCLI->GetValue());*/
    wxString AS =  iCLI->GetValue();
    AS << _("\n");
    iPAST->AppendText( AS );
    iCLI->Clear();

}
